using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class TinhTrang_SanPham_DAL
    {
        private SqlConnection cn = null;
        public  TinhTrang_SanPham_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(TinhTrang_SanPham_DTO Tinhtrang_sanpham)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_SanPham_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_SP",Tinhtrang_sanpham .MaTinhTrang_SP  );
            cm.Parameters.AddWithValue("@TenTinhTrang",Tinhtrang_sanpham .TenTinhTrang );
                     
            cm.ExecuteNonQuery();
        }
        public void Update(TinhTrang_SanPham_DTO Tinhtrang_sanpham)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_SanPham_Upsate", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_SP", Tinhtrang_sanpham.MaTinhTrang_SP);
            cm.Parameters.AddWithValue("@TenTinhTrang", Tinhtrang_sanpham.TenTinhTrang);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_SanPham_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_SP", iD);
            cm.ExecuteNonQuery();
        }
        public TinhTrang_SanPhamCollection   Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_SanPham_SET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            TinhTrang_SanPhamCollection   TinhtrangsanphamCollection = new TinhTrang_SanPhamCollection  ();

            while (dr.Read ())
            {
                TinhTrang_SanPham_DTO tinhtrangsanpham = new TinhTrang_SanPham_DTO  ();
                tinhtrangsanpham .MaTinhTrang_SP   = int.Parse(dr.GetValue(0).ToString());
                tinhtrangsanpham .TenTinhTrang   = dr.GetValue(1).ToString();

                TinhtrangsanphamCollection.Add(tinhtrangsanpham);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return TinhtrangsanphamCollection ;
        }
    }
}
